#include "Gomba.h"

Gomba::Gomba()
{
	_vx=0.0000000000f;
	_vy=0.f;
}
Gomba::Gomba(float x, float y, float vx, float vy, int deltaTime,int resourceName,CResourceManager *rm):MObject(x,y,vx,vy,deltaTime,resourceName,rm)
{
	_objectType = CResourceManager::TYPE_MUSHROOM;
	_isMovable = true;
	_vx = -0.07f;
	_spriteMove = rm->getSprite(CResourceManager::GOMBAMOVE);
	_spritePushed = rm->getSprite(CResourceManager::GOMBAPUSHED);
	_spriteThrowing = rm->getSprite(CResourceManager::GOMBADIE);
	_dyingTime = deltaTime * 30;
	_status = STATUS_ALIVE;
	_timeScore = 100;
	_info = CInfo(rm);
	_scored = 100;
	_timeScore = 0;
}
Gomba::Gomba(float x, float y, int Width, int Height, float vx, float vy, int deltaTime, LPD3DXSPRITE spriteHandler, LPDIRECT3DTEXTURE9 image):MObject(x,y,Width,Height,vx,vy,deltaTime,spriteHandler,image)
{
	
	_isMovable = true;

}
Gomba::~Gomba(void)
{

}

void Gomba::Move()
{
	switch (_status)
	{
	case STATUS_ALIVE:
		{
			float ty = _lastTranslateVector.getY() + this->getVelocityVector().getY();
	 
			if (_isFalling == false || this->getLastTranslateVector() != Vector() &&(ty<=0))
				{
					_vy = 0; 
				}
				else
					if (_vy < 1.5f)
   						_vy += 0.15f;

			if (_y >600 - 32) 
				setStatus(STATUS_DEAD);

			_lastX = _x;
			_lastY = _y;

			_x +=_vx*_deltaTime;
			_y +=_vy*_deltaTime;

			_lastTranslateVector = Vector();
			break;
		}
	case STATUS_DYING:
		{
			_dyingTime -= _deltaTime;
			
			if (_dyingTime <= 0)
				setStatus(STATUS_DEAD);

			break;
		}
	case STATUS_THROWING:
		{
			float ty = _lastTranslateVector.getY() + this->getVelocityVector().getY();

	 
					if (_vy < 1.5f)
   						_vy += 0.15f;

			if (_y >600 - 32)
				setStatus(STATUS_DEAD);

			_lastX = _x;
			_lastY = _y;

			_x +=_vx*_deltaTime;
			_y +=_vy*_deltaTime;

			_lastTranslateVector = Vector();
			break;
		}

	}
}

void Gomba::setVelocity(Vector v, shared_ptr<MObject> Object)
{
	//Phai de trong.
}

void Gomba::Render()
{
	Vector v = CScreen::getPosition();
	this->_sprite->Render(this->_x - v.getX(),this->_y - v.getY());
	if (_timeScore > 0)
	{
		_info.RenderNumber(100,_x - v.getX(), _ty - v.getY());
		_timeScore--;
		_ty -= (0.05f*_deltaTime);
	}
}

void Gomba::actAfterCollided(int Direction, shared_ptr<MObject> Object)
{
	if (Object->getObjectType() == CResourceManager::TYPE_BULLET)
	{
		setStatus(STATUS_THROWING);
		Object->setStatus(CBullet::STATUS_DEAD);
		return;
	}

	switch(Direction)
	{
	case 1://UP
		{
			_vy = 0;
			if (Object->getObjectType() == CResourceManager::TYPE_BRICK && (Object->getStatus() == Brick::STATUS_SLIDE || Object->getStatus() == Brick::STATUS_BREAKED))
				setStatus(STATUS_THROWING);
			break;
		}
	case 2: //DOWN
		{
			if ((Object->getObjectType()/10) == CResourceManager::TYPE_MARIO)
			{
				setStatus(STATUS_DYING);
				//Object->setScore(Object->getScore() + 100);
			}
				//MessageBox(NULL,"I'm Dead!","Haha",MB_OK);
			break;
		}
	case 3: //RIGHT
	case 4: //LEFT
		{
			if (_recentChangeVel == false)
			{
				if (Object->getPosition().getY() <= _y + 20)
				{
					_vx = -_vx;
					_recentChangeVel = true;
				}
			}
			else _recentChangeVel = false;
			if (Object->getObjectType() == CResourceManager::TYPE_TURTLE && Object->getStatus() == Turtle::STATUS_PUSHED && Object->getVelocityVector().getX() != 0)
			{
				setStatus(STATUS_THROWING);
				//Object->setScore(Object->getScore() + 100);
			}
			break;
		}
	case 0:
		break;
	}
}

void Gomba::setStatus(int Status)
{
	_status = Status;
	switch (_status)
	{
		case STATUS_ALIVE:
			break;
		case STATUS_DYING:
			{
				_sprite = _spritePushed;
				_timeScore = 100;
				_ty = _y;
				CMarioSound::getInstance()->Play(CMarioSound::SOUND_ENEMYDIE,false);

			}

			break;
		case STATUS_THROWING:
			{
				_sprite = _spriteThrowing;
				_vx = 0;
				_vy = -1.3f;
				_timeScore = 100;
				_ty = _y;
				CMarioSound::getInstance()->Play(CMarioSound::SOUND_ENEMYDIE,false);

			}

		case STATUS_DEAD:
			break;
	}
}